package com.hqyj.hrms.modules.personnel.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.hrms.modules.personnel.dao.EmployeeMapper;
import com.hqyj.hrms.modules.personnel.dao.TransferMapper;
import com.hqyj.hrms.modules.personnel.entity.Employee;
import com.hqyj.hrms.modules.personnel.entity.Record;
import com.hqyj.hrms.modules.personnel.entity.Transfer;
import com.hqyj.hrms.modules.personnel.service.TransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @Autor zyj
 * @Date 2021/10/2
 **/
@Service
public class TransferServiceImpl implements TransferService {
    @Autowired(required = false)
    TransferMapper transferMapper;
    @Autowired(required = false)
    EmployeeMapper employeeMapper;

    @Override
    public HashMap<String, Object> list(Transfer transfer) {
        HashMap<String, Object> map = new HashMap<String, Object>();
//        //创建查询插件对象
//        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
//
//        //条件   列名   参数
//        queryWrapper.eq(
//                employee.getEmpName() != null && !employee.getEmpName().equals(""),
//                "emp_name",
//                employee.getEmpName());

        String keyWord = transfer.getKeyWord();
        QueryWrapper wrapper = new QueryWrapper<Record>()
                .like(StringUtils.isNotBlank(keyWord), "transfer_id", keyWord).or()
                .like(StringUtils.isNotBlank(keyWord), "emp_id", keyWord).or()
                .like(StringUtils.isNotBlank(keyWord), "old_department", keyWord).or()
                .like(StringUtils.isNotBlank(keyWord), "old_job", keyWord).or()
                .like(StringUtils.isNotBlank(keyWord), "new_department", keyWord).or()
                .like(StringUtils.isNotBlank(keyWord), "new_job", keyWord).or()
                .like(StringUtils.isNotBlank(keyWord), "transfer_date", keyWord).or()
                .like(StringUtils.isNotBlank(keyWord), "transfer_reason", keyWord);

        //第一步创建分页对象
        Page<Transfer> transferPage = new Page<>(transfer.getPage(), transfer.getRow());
        //第二页  查询分页  获取分页数据集
        Page<Transfer> pageList = transferMapper.selectPage(transferPage, wrapper);
        //获取当前页的集合
        map.put("info", pageList.getRecords());
        //获取总条数
        map.put("total", pageList.getTotal());
        //获取总页数
        map.put("totalPage", pageList.getPages());
//        List<UserInfo> list = userInfoMapper.selectList(null);
//        map.put("info",list);
        return map;
    }

    @Override
    public HashMap<String, Object> add(Transfer transfer) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        employeeMapper.updateEmployee(transfer);
        int num = transferMapper.insert(transfer);
        if (num > 0) {
            map.put("info", "保存成功");
        } else {
            map.put("info", "失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(Transfer transfer) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        employeeMapper.updateEmployee(transfer);
        int num = transferMapper.updateById(transfer);
        if (num > 0) {
            map.put("info", "保存成功");
        } else {
            map.put("info", "失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer id) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = transferMapper.deleteById(id);
        if (num > 0) {
            map.put("info", "删除成功");
        } else {
            map.put("info", "删除失败");
        }
        return map;
    }
}
